package com.fly.lambda.service;

import com.fly.lambda.dao.ArticleDAO;
import com.fly.lambda.domain.Article;
import com.fly.lambda.framework.dao.Pagination;
import com.fly.lambda.framework.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Fly on 2016/8/1.
 */
@Service
public class ArticleService {

    @Autowired
    ArticleDAO articleDAO;

    /**
     * 查询某一栏目下的文章
     * @param channelId 栏目id
     * @param curPage   当前页
     * @param pageSize  每页显示
     * @return
     */
    public Pagination<Article> queryArticleByChannel(String channelId, int curPage, int pageSize){
        return articleDAO.queryArticleByChannel(channelId, curPage, pageSize);
    }

    /**
     * 获取推荐的文章列表
     * @param count 显示的文章数量
     * @return
     */
    public List<Article> getRecommendArticle(int count){
        return articleDAO.getRecommendArticle(count);
    }

    /**
     * 获取相关文章列表
     * @param channelId 栏目id
     * @param articleId 文章id
     * @param count 显示数量
     * @return
     */
    public List<Article> getRelationArticle(String channelId, String articleId, int count){
        return articleDAO.getRelationArticle(channelId, articleId, count);
    }

    /**
     * 保存文章信息
     * @param article
     */
    public void saveArticle(Article article){
        if (StringUtils.isBlank(article.getArticleId())){
            article.setArticleId(CommonUtils.genPK());
            articleDAO.create(article);
        }
        else{
            articleDAO.update(article);
        }
    }

    /**
     *  根据id获取文章
     * @param articleId 文章id
     * @return
     */
    public Article getById(String articleId){
        Article article = articleDAO.getById(articleId);
        if (null == article){
            article = new Article();
        }
        return article;
    }

    /**
     * 根据id删除文章
     * @param articleId 文章id
     */
    public void deleteById(String articleId){
        articleDAO.deleteById(articleId);
    }
}
